import { createStore, applyMiddleware } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import createSagaMiddleware from 'redux-saga';
import storage from 'redux-persist/lib/storage'; // or whatever storage you are using

import rootReducer from '../reducer';
import saga from '../saga';

const config = {
  key: 'primary',
  storage,
  whitelist: ['coreData'],
};
const reducer = persistReducer(config, rootReducer);
const sagaMiddleware = createSagaMiddleware();
const store = createStore(reducer, applyMiddleware(sagaMiddleware));
function getStore() {
  return store;
}

export default function configureStore(callback) {
  sagaMiddleware.run(saga);
  persistStore(store, null, callback);

  return store;
}

export { getStore };
